package managedbean.SCM.PickingOrder;

import entity.CRM.LineItem;
import entity.SCM.DeliveryOrder;
import entity.SCM.ProductBalance;
import entity.SCM.ProductLine;
import java.io.IOException;
import javax.faces.event.ActionEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import session.stateless.SCM.DeliveryOrderBeanLocal;

@ManagedBean(name="addPickingOrderManagerBean")
@ViewScoped

public class AddPickingOrderManagerBean implements Serializable {
    
    public AddPickingOrderManagerBean () {
    }
    
    @EJB
    private DeliveryOrderBeanLocal deliveryOrderBeanLocal;
    private DeliveryOrder selectedDeliveryOrder;
    private List<ProductLine> productLines;
    private String statusMessage;
    private Double finalPrice;

    public void initView(PhaseEvent event)
    {        
        if (selectedDeliveryOrder == null)  
        {
            selectedDeliveryOrder = (DeliveryOrder) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedDeliveryOrder");                          
            List<LineItem> temp_LineItems = selectedDeliveryOrder.getSaleOrder().getLineItems();
            
            List<ProductLine> temp_ProductLines = new ArrayList<ProductLine>();
            for (LineItem temp:temp_LineItems)
            {
                ProductLine productLine = new ProductLine();
                
                ProductBalance tempProduct= deliveryOrderBeanLocal.findProduct(temp.getProductPrice().getProductType());
                productLine.setProduct(tempProduct);
                productLine.setQuantity(temp.getLineItemQuantity());
                productLine.setFinalPrice(temp.getFinalPrice());
                
                temp_ProductLines.add(productLine);            
            }
            
            this.setProductLines(temp_ProductLines);
        }
    }

    public List<ProductLine> getProductLines() {
        return productLines;
    }

    public void setProductLines(List<ProductLine> productLines) {
        this.productLines = productLines;
    }
    
    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public DeliveryOrder getSelectedDeliveryOrder() {
        return selectedDeliveryOrder;
    }

    public void setSelectedDeliveryOrder(DeliveryOrder selectedDeliveryOrder) {
        this.selectedDeliveryOrder = selectedDeliveryOrder;
    }

    public Double getFinalPrice() {
        return finalPrice;
    }

    public void setFinalPrice(Double finalPrice) {
        this.finalPrice = finalPrice;
    }
    
    public void saveNewPickingOrder(ActionEvent event)throws IOException
    {
        List<LineItem> temp_Lineitems = selectedDeliveryOrder.getSaleOrder().getLineItems();
        for (int i=0;i<productLines.size();i++)
        {
            Double temp_Price=temp_Lineitems.get(i).getFinalPrice()*productLines.get(i).getQuantity() /temp_Lineitems.get(i).getLineItemQuantity();
            productLines.get(i).setFinalPrice(temp_Price);
        }
        
        List<ProductLine> temp_productLines = new ArrayList<ProductLine>();
        for (ProductLine temp:productLines)
        {
            temp_productLines.add(deliveryOrderBeanLocal.createProductLine(temp.getProduct().getProductType(), temp.getQuantity(), temp.getFinalPrice()));        
        }
        
        Long pickingOrderId= deliveryOrderBeanLocal.createPickingOrder(selectedDeliveryOrder.getId(), temp_productLines);
        
        statusMessage = "New Picking Order with ID "+pickingOrderId+" Saved Successfully";

        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Add New Picking Order Result: " + statusMessage, ""));
            
    }
    
    public void calculate(ActionEvent event) throws IOException
    {
        List<LineItem> temp_Lineitems = selectedDeliveryOrder.getSaleOrder().getLineItems();
        Double temp_FinalPrice= 0.0;
        for (int i=0;i<productLines.size();i++)
        {
            Double temp_Price=temp_Lineitems.get(i).getFinalPrice()*productLines.get(i).getQuantity() /temp_Lineitems.get(i).getLineItemQuantity();
            productLines.get(i).setFinalPrice(temp_Price);
            temp_FinalPrice+=temp_Price;
        }
        this.setFinalPrice(temp_FinalPrice);
        System.out.println(finalPrice);
    }
}
